// index.js
import { generateDates, getCurrentDate, parseDateString } from './utils'
import systemBehavior from '../../behaviors/system'

const selectedDays = generateDates()
const currentDate = getCurrentDate()

Page({
  behaviors: [systemBehavior],
  data: {
    visible: false,
    selectedDays,
    current: currentDate[0].date,
    scrollIntoView: `id-${currentDate[0].date}`,
  },
  handleShowCalendar() {
    this.setData({
      visible: !this.data.visible,
    })
  },
  handleConfirm(event) {
    const tmpVal = this.getDateValue(event.detail, false)
    const selectedDays = generateDates(parseDateString(tmpVal), true)
    const selectedDate = this.getDateValue(event.detail)

    this.setData(
      {
        current: selectedDate,
        selectedDays,
      },
      () => {
        this.setData({ scrollIntoView: `id-${selectedDate}` })
      }
    )
    this.handleShowCalendar()
  },
  chooseDate(event) {
    const { item } = event.currentTarget.dataset
    this.setData({
      current: item.date,
    })
  },
  getDateValue(date, isRemoveZero = true, symbol = '-') {
    if (isRemoveZero) {
      date = new Date(date)
      return `${date.getMonth() + 1}${symbol}${date.getDate()}`
    } else {
      date = new Date(date)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
      const day = String(date.getDate()).padStart(2, '0')
      
      return year + '-' + month + '-' + day;

    }
  }
})
